package com.example.mywanandroid.home.business;
import android.util.Log;

import com.example.mywanandroid.util.HttpUtil;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class GetData {
    public static void getViewPagerData(final ViewPagerCallBack callBack) {
        String requestBanner = "https://www.wanandroid.com/banner/json";
        HttpUtil.sendOkHttpRequest(requestBanner, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                //如果获取数据失败传回null
                callBack.onGetViewPagerData(null);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String bannerData = response.body().string();
                Log.i("networkResponse:", response.networkResponse() + "");
                Log.i("cacheResponse:", response.cacheResponse() + "");
                response.body().close();
                callBack.onGetViewPagerData(bannerData);
            }
        });
    }

    public static void getRecyclerViewData(int page, final RecyclerViewCallBack callBack) {
        String requestArticle = "https://www.wanandroid.com/article/list/" + page + "/json";
        HttpUtil.sendOkHttpRequest(requestArticle, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                //如果获取数据失败传回null
                callBack.onGetRecyclerViewData(null);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String articleData = response.body().string();
                Log.i("networkResponse:", response.networkResponse() + "");
                Log.i("cacheResponse:", response.cacheResponse() + "");
                response.body().close();
                callBack.onGetRecyclerViewData(articleData);
            }
        });
    }
}
